﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Web.Caching;
using Aandelendelen.Caching;

namespace Aandelendelen.Obs
{
	public class MutationDescription : Dictionary<string, string>, ICacheable
	{
		public static string CacheName = "data_mutationdescription"; 

		public MutationDescription()
		{
			InitCacheKey();

			this.Read();
		}

		private void Read()
		{
			XmlDocument doc = new XmlDocument();

			doc.Load(HttpContext.Current.Server.MapPath("/App_Data/MutationDescription.xml"));

			XmlNode node = doc.DocumentElement;

			foreach (XmlNode childNode in node.ChildNodes)
			{
				if (childNode.Name == "mutation")
				{
					string key = childNode.Attributes["value"].Value;
					string value = childNode.Attributes["description"].Value;

					this.Add(key, value);
				}
			}
		}

		public static MutationDescription GetInstance()
		{
			return DataCache.GetCacheableObject<MutationDescription>(CacheName);
		}

		public static MutationDescription GetInstance(string cacheKey)
		{
			return DataCache.GetCacheableObject<MutationDescription>(cacheKey);
		}

		#region ICacheable
		public CacheKey CacheKey { get; set; }

		public void CacheThis(string cacheKey)
		{
			this.CacheKey.CacheThis(cacheKey);
		}

		public void InitCacheKey()
		{
			CacheKey = new CacheKey()
			{
				CacheDependency = new CacheDependency(HttpContext.Current.Server.MapPath("/App_Data/MutationDescription.xml")),
				ObjectToCache = this
			};
		}
		#endregion

	}
}